package com.colin.view;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.colin.R;
import com.colin.base.BaseActivity;
import com.colin.constant.Constants;
import com.colin.mvp.download.presenter.MainPresenter;
import com.colin.mvp.download.presenter.MainPresenterImpl;
import com.colin.system.CameraUtil;
import com.colin.system.PermissionUtil;
import com.colin.utils.FileUtil;
import com.colin.utils.LogUtil;
import com.colin.utils.StrKit;
import com.qmuiteam.qmui.alpha.QMUIAlphaButton;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends BaseActivity implements MainView {

	@BindView(R.id.download)
	QMUIAlphaButton mDownload;
	@BindView(R.id.open)
	QMUIAlphaButton mOpen;
	@BindView(R.id.upload)
	QMUIAlphaButton mUpload;
	@BindView(R.id.capture)
	QMUIAlphaButton mCapture;
	@BindView(R.id.image)
	ImageView mImageView;


	private MainPresenter mMainPresenter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ButterKnife.bind(this);
		Constants.apkPath = FileUtil.generateDownloadPath(this, Constants.apkName);
		mMainPresenter = new MainPresenterImpl(this);
	}


	File cameraFile;
	Uri saveUri;

	@OnClick({R.id.download, R.id.open, R.id.upload, R.id.capture})
	public void onViewClicked(View view) {
		Intent intent = new Intent(Intent.ACTION_VIEW);

		switch (view.getId()) {
			case R.id.download:
				mMainPresenter.downloadFile(Constants.apkUrl);
				break;
			case R.id.open:
//				File file = new File(Constants.apkPath);
//				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				intent.setDataAndType(Uri.fromFile(file), FileUtil.getMIMEType(file));
//				startActivityForResult(intent, Constants.REQUEST_INSTALL);
				//PackageUtils.install(this,Constants.apkPath);
				break;
			case R.id.upload:
				Intent intent1 = new Intent();
				intent1.setType("*/*");
				intent1.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(intent1, Constants.REQUEST_ALBUM);
				break;
			case R.id.capture:
				if (PermissionUtil.applyWritePermission(this)) {
					cameraFile = new File(Environment.getExternalStorageDirectory() + File.separator + getPackageName() + File.separator
						+ System.currentTimeMillis() + ".jpg");
					LogUtil.e("cameraFile = " + cameraFile);
					cameraFile.getParentFile().mkdirs();
					//改变Uri  com.xykj.customview.fileprovider注意和xml中的一致
					saveUri = FileProvider.getUriForFile(this, getPackageName()+".fileprovider", cameraFile);
					CameraUtil.useCamera(this, saveUri);
				}
				break;
		}
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case Constants.REQUEST_ALBUM:
					handleImageOnKitKat(data);
					break;
				case Constants.REQUEST_CAMERA:
					ContentResolver contentProvider = getContentResolver();
					ParcelFileDescriptor mInputPFD;
					try {
						//获取contentProvider图片
						mInputPFD = contentProvider.openFileDescriptor(saveUri, "r");
						FileDescriptor fileDescriptor = mInputPFD.getFileDescriptor();
						mImageView.setImageBitmap(BitmapFactory.decodeFileDescriptor(fileDescriptor));
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
					break;
			}
		}
	}


	@SuppressLint("NewApi")
	private void handleImageOnKitKat(Intent data) {
		String imagePath = null;
		Uri uri = data.getData();
		imagePath = FileUtil.getPathByUri(this, uri);
		LogUtil.e("imagePath :" + imagePath);
		if (StrKit.notBlank(imagePath)) {
			mMainPresenter.uploadFiles(new File(imagePath));
			ImageView imageView = findViewById(R.id.image);
			try {
				imageView.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(new File(imagePath))));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			Toast.makeText(this, "imagePath:" + imagePath, Toast.LENGTH_SHORT).show();
		}
	}
}
